#!/bin/bash
set -e

function print_help
{
    echo "sh build.sh                                 # 编译，代码目录为环境变量CODESPACE_SERVER"
    echo "sh build.sh /data/openGauss-server          # 编译，指定代码目录"
    echo "sh build.sh -h                              # 帮助"
}
if [ "$1" = "-h" ]; then
    print_help
    exit 0
fi

# 找到openGauss-server代码目录
code_src=""
if [ "$1" != "" ]; then
    code_src=$1
else
    code_src=$CODESPACE_SERVER
fi
if [ not -d $code_src ]; then
    echo "找不到openGauss-server代码目录。参考："
    print_help
    exit 1
fi


extension_dir=$code_src/contrib/devinsight

echo "rm $extension_dir -fr"
rm $extension_dir -fr

echo "mkdir $extension_dir"
mkdir $extension_dir

echo "cp ./* $extension_dir/ -r"
cp ./* $extension_dir/ -r

echo "cd $extension_dir"
cd $extension_dir

echo "make && make install"
make -sj
make install -j

cd -


echo "create command:"
echo "    create extension devinsight;"
echo "    select * from devinsight();"
